package com.hjq.mallcoupon.service.impl;

import com.hjq.common.entity.to.MemberPrice;
import com.hjq.common.entity.to.SkuReductionTO;
import com.hjq.mallcoupon.entity.MemberPriceEntity;
import com.hjq.mallcoupon.entity.SkuLadderEntity;
import com.hjq.mallcoupon.service.MemberPriceService;
import com.hjq.mallcoupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjq.common.utils.PageUtils;
import com.hjq.common.utils.Query;

import com.hjq.mallcoupon.dao.SkuFullReductionDao;
import com.hjq.mallcoupon.entity.SkuFullReductionEntity;
import com.hjq.mallcoupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuLadderService skuLadderService;
    @Autowired
    MemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTO skuReductionTO) {
        //1.保存满减打折和会员价 gulimall-sms->sms_sku_ladder/sms_sku_full_reduction/sms_member_price/

        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuReductionTO.getSkuId());
        skuLadderEntity.setFullCount(skuReductionTO.getFullCount());
        skuLadderEntity.setDiscount(skuReductionTO.getDiscount());
        skuLadderEntity.setAddOther(skuReductionTO.getCountStatus());

        if(skuLadderEntity.getFullCount()>0){
            skuLadderService.save(skuLadderEntity);
        }

        //sms_sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTO,skuFullReductionEntity);
        if(skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0"))>0){
            this.save(skuFullReductionEntity);
        }



        //sms_member_price
        List<MemberPrice> memberPriceList = skuReductionTO.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntities = memberPriceList.stream().map(memberPrice -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuReductionTO.getSkuId());
            memberPriceEntity.setMemberLevelId(memberPrice.getId());
            memberPriceEntity.setMemberPrice(memberPrice.getPrice());
            memberPriceEntity.setMemberLevelName(memberPrice.getName());
            memberPriceEntity.setAddOther(1);//默认叠加
            return memberPriceEntity;
        }).filter(item->
            item.getMemberPrice().compareTo(new BigDecimal("0"))>0
        ).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);




    }

}